package com.myradio.principale;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.erkutaras.media.audio.url.R;

public class Main extends Activity {
	private TextView lbl_streaming;
	private String lbl_streaming_text;
	private Button btn_play, btn_stop;
	private MediaPlayer mediaPlayer;
	private String url_lq = "http://94.23.62.189:5542/stream/2/";
	private String url_hq = "http://94.23.62.189:5542/stream/1/";
	public Utils utils = new Utils(this);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init() {
		lbl_streaming = (TextView) findViewById(R.id.streaming_status);
		lbl_streaming.setText(Utils.LETTORE_PRONTO);
		
		btn_play = (Button)findViewById(R.id.btn_play);
		btn_play.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if(utils.checkConnection() != null){

					mediaPlayer = new MediaPlayer();
					mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

					if(utils.isMobile()){
						try {
							mediaPlayer.setDataSource(url_lq);
							lbl_streaming_text = utils.MOD_RISPARMIO;
						} catch (Exception e) {
							utils.showAlert(utils.ERRORE, utils.IMPOS_LEGGERE_LQ);
						}
					} else {
						try {
							mediaPlayer.setDataSource(url_hq);
							lbl_streaming_text = utils.MOD_QUALITA;
						} catch (Exception e) {
							utils.showAlert(utils.ERRORE, utils.IMPOS_LEGGERE_HQ);
						}
					}
					
					start();
					
				} else {
					utils.showAlert(utils.ERRORE, utils.VERIF_CONNESSIONE);
				}


			}
		});

		btn_stop = (Button)findViewById(R.id.btn_stop);
		btn_stop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				stop();
			}
		});

		btn_stop.setEnabled(false);

	}

	private void start(){	
		mediaPlayer.prepareAsync();
		final ProgressDialog dialog = ProgressDialog.show(utils.getContesto(), utils.ATTENDI, utils.CARICAMENTO);
		
		mediaPlayer.setOnErrorListener(new OnErrorListener() {
			
			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				dialog.hide();
				if(lbl_streaming_text.equals(utils.MOD_QUALITA)){
					utils.showAlert(utils.ERRORE, utils.IMPOS_LEGGERE_HQ);
					return true;
				} else if(lbl_streaming_text.equals(utils.MOD_RISPARMIO)){
					utils.showAlert(utils.ERRORE, utils.IMPOS_LEGGERE_LQ);
					return true;
				}
				return false;
			}
		});
		
		
		mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				mediaPlayer.start();
				btn_play.setEnabled(false);
				btn_stop.setEnabled(true);
				dialog.hide();
				lbl_streaming.setText(Html.fromHtml(lbl_streaming_text));
			}
		});
	}

	private void stop(){
		mediaPlayer.stop();
		mediaPlayer.reset();
		btn_play.setEnabled(true);
		btn_stop.setEnabled(false);
		lbl_streaming.setText(Html.fromHtml(utils.LETTORE_PRONTO));
	}

}